<?php
if(!defined('PUBLIC_DIR')) die('No Acces!');

class File implements iFile {
	private $Path = '';
	private $Info = array();
	private $Dir = null;
	private $Handle = null;
	private $Content = array();

	public function __construct($_path, $_createIfNotExits = false){
		if($_createIfNotExits && !is_file($_path) && is_dir(basename($_path)))
			if(!touch($_path))
				trigger_error($_path . ' is not path to file!', E_USER_ERROR);
		if(is_file($_path))
			$this->Path = realpath($_path);
		else
			$this->Path = $_path;
	}

	public function Refresh(){
		$this->Info = array();
		$this->Dir = null;
	}
	public function ChangeFile($_path, $_createIfNotExits = false){
		if($_createIfNotExits && !is_file($_path) && is_dir(basename($_path)))
			if(!touch($_path))
				trigger_error($_path . ' is not path to file!', E_USER_ERROR);
		if(is_file($_path))
			$this->Path = realpath($_path);
		else
			$this->Path = $_path;
		$this->Refresh();
	}

	public function GetPath(){
		return $this->Path;
	}
	public function GetBasename(){
		if(!is_array($this->Info) || !isset($this->Info['basename']))
			return $this->Info = pathinfo($this->Path);
		return $this->Info['basename'];
	}
	public function GetFilename(){
		if(!is_array($this->Info) || !isset($this->Info['filename']))
			return $this->Info = pathinfo($this->Path);
		return $this->Info['filename'];
	}
	public function GetExtension(){
		if(!is_array($this->Info) || !isset($this->Info['extension']))
			return $this->Info = pathinfo($this->Path);
		return $this->Info['extension'];
	}
	public function GetDir(){
		return new Dir($this->GetDirname());
	}
	public function GetDirname(){
		if(!is_array($this->Info) || !isset($this->Info['dirname']))
			return $this->Info = pathinfo($this->Path);
		return $this->Info['dirname'];
	}
	public function GetDirpath(){
		if(!is_array($this->Info) || !isset($this->Info['dirname']))
			return $this->Info = pathinfo($this->Path);
		return basename($this->Info['dirname']);
	}

	public function GetSize(){
		if(!is_array($this->Info) || !isset($this->Info['size']))
			return $this->Info['size'] = filesize($this->Path);
		return $this->Info['size'];
	}
	public function GetDate($_type = 'Create'){
		if($_type == 'Create' || $_type == 'c'){
			if(!is_array($this->Info) || !isset($this->Info['date_c']))
				return $this->Info['date_c'] = filectime($this->Path);
			return $this->Info['date_c'];
		}else if($_type == 'Modify' || $_type == 'm'){
			if(!is_array($this->Info) || !isset($this->Info['date_m']))
				return $this->Info['date_m'] = filemtime($this->Path);
			return $this->Info['date_m'];
		}else if($_type == 'Access' || $_type == 'a'){
			if(!is_array($this->Info) || !isset($this->Info['date_a']))
				return $this->Info['date_a'] = fileatime($this->Path);
			return $this->Info['date_a'];
		}
	}
	public function GetMode(){
		$perms = fileperms($this->Path);

		if (($perms & 0xC000) == 0xC000) {
			// Socket
			$info = 's';
		} elseif (($perms & 0xA000) == 0xA000) {
			// Symbolic Link
			$info = 'l';
		} elseif (($perms & 0x8000) == 0x8000) {
			// Regular
			$info = '-';
		} elseif (($perms & 0x6000) == 0x6000) {
			// Block special
			$info = 'b';
		} elseif (($perms & 0x4000) == 0x4000) {
			// Directory
			$info = 'd';
		} elseif (($perms & 0x2000) == 0x2000) {
			// Character special
			$info = 'c';
		} elseif (($perms & 0x1000) == 0x1000) {
			// FIFO pipe
			$info = 'p';
		} else {
			// Unknown
			$info = 'u';
		}

		// Owner
		$info .= (($perms & 0x0100) ? 'r' : '-');
		$info .= (($perms & 0x0080) ? 'w' : '-');
		$info .= (($perms & 0x0040) ?
					(($perms & 0x0800) ? 's' : 'x' ) :
					(($perms & 0x0800) ? 'S' : '-'));

		// Group
		$info .= (($perms & 0x0020) ? 'r' : '-');
		$info .= (($perms & 0x0010) ? 'w' : '-');
		$info .= (($perms & 0x0008) ?
					(($perms & 0x0400) ? 's' : 'x' ) :
					(($perms & 0x0400) ? 'S' : '-'));

		// World
		$info .= (($perms & 0x0004) ? 'r' : '-');
		$info .= (($perms & 0x0002) ? 'w' : '-');
		$info .= (($perms & 0x0001) ?
					(($perms & 0x0200) ? 't' : 'x' ) :
					(($perms & 0x0200) ? 'T' : '-'));

		echo $info;
	}
	public function GetOwner(){
		if(!is_array($this->Info) || !isset($this->Info['owner']))
			return $this->Info['owner'] = fileowner($this->Path);
		return $this->Info['owner'];
	}
	public function GetGroup(){
		if(!is_array($this->Info) || !isset($this->Info['group']))
			return $this->Info['group'] = filegroup($this->Path);
		return $this->Info['group'];
	}

	public function Open(){
		if(!is_resource($this->Handle))
			return $this->Handle = fopen($this->Path, 'a+');
		else
			return true;
	}
	public function Read($Position = 0){
		
	}
	public function ReadLine($Line = 0){
		if(!is_array($this->Content))
			$this->ReadLines();
		if(isset($this->Contetn[$_line]))
			return $this->Contetn[$_line];
		return False;
	}
	public function ReadLines(){
		return $this->Content = file($this->Path);
	}
	public function Write($Content){
	}
	public function WriteLine($Line){
		$this->Content[] = $Line;
	}
	public function WriteLines(array $Content){
		$this->Content = $Content;
	}
	public function Append($Content, $Position = -1){
	}
	public function AppendLine($Line, $Position = -1){
		if(!is_array($this->Content))
			$this->WriteLines();
		if($Position == -1)
			return $this->Content[] = $Line;
	}
	public function AppendLines(array $Lines){
		$this->Content = array_merge($this->Content, $Lines);
	}
	public function Save(){
		if(!is_resource($this->Handle))
			$this->Open();
		return fwrite($this->Content);
	}
	public function Clear(){
		$this->Content = array();
	}
	public function Close(){
		if(is_resource($this->Handle))
			return fclose($this->Handle);
		else
			return true;
	}

	public function IsExits(){
		if(!is_array($this->Info) || !isset($this->Info['exits']))
			return $this->Info['exits'] = file_exists($this->Path);
		return $this->Info['exits'];
	}
	public function IsRedable(){
		if(!is_array($this->Info) || !isset($this->Info['readable']))
			return $this->Info['readable'] = is_readable($this->Path);
		return $this->Info['readable'];
	}
	public function IsWritable(){
		if(!is_array($this->Info) || !isset($this->Info['writable']))
			return $this->Info['writable'] = is_writable($this->Path);
		return $this->Info['writable'];
	}
	public function CheckAccess(){

	}

	public function Create(){
		return touch($this->Path);
	}
	public function Move($oldPath, $newPath){
		$value = rename($oldPath, $newPath);
		if($value) $this->ChangeFile($newPath);
		return $value;
	}
	public function Rename($oldName, $newName){
		$value = rename($oldName, $newName);
		//if($value) $this->ChangeFile(str_replace()$newPath);
		return $value;
	}
	public function Copy($dPath, $sPath = null){
		if($sPath == null)
			$sPath = $this->Path;
		return copy($sPath, $dPath);
	}
	public function Delete(){
		return unlink($this->Path);
	}
}